//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    categorys: [],
    goods: [],
    items:[],
    checkedId: 1
  },

  onLoad: function () {

    let picArray=[
      "http://assets.wxappclub.com/lesson/mall/imgs/goods_1_4.jpg",
      "http://assets.wxappclub.com/lesson/mall/imgs/goods_1_5.jpg",
      "http://assets.wxappclub.com/lesson/mall/imgs/goods_2_5.jpg",
      "http://assets.wxappclub.com/lesson/mall/imgs/goods_2_3.jpg",
      "http://assets.wxappclub.com/lesson/mall/imgs/goods_2_4.jpg"
    ];
    let categorys=[];
    let goods = [];
    for (let i = 1; i < 10; i++) {
      categorys.push({id:i,name:"分类"+i});
      let itemArray=[];
      for (let j = i; j < i * 19; j++) {
        itemArray.push({
          id: j,
          name: "商品"+j,
          price: Math.floor(Math.random() * 100),
          pic:picArray[Math.floor(Math.random() * picArray.length)]
        });
      }
      goods.push({
        categoryId: i,
        items: itemArray
      });
    }
    this.setData({
      categorys:categorys,
      goods: goods,
      items:goods[0].items
    });


  },
  changeCat:function(e){
    let catId=e.target.dataset.id;
    let goods = this.data.goods;
    for(let key in goods){
      if(goods[key].categoryId==catId){
        this.setData({
          checkedId: catId,
          items:goods[key].items
        });
      }
    }
  }

})